# -*- coding:utf-8 -*-
import json
import random
import time
from ssl import _create_unverified_context
from json import loads

import jieba.analyse as analyse
import pyperclip as pyperclip
from lxml import etree
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import tkinter.messagebox

import urllib.request
import urllib.parse

storeContent='<p align="right" style="text-align: center;"><img src="//img30.360buyimg.com/popWareDetail/jfs/t1/223758/23/23489/97293/6456658bFd9e9877d/de6f45854107f173.jpg!q70.dpg.webp" class="" loaded="43" style="max-width: 390px;"><img src="//img30.360buyimg.com/popWareDetail/jfs/t1/108410/32/41072/131776/64773719Fc0054c8b/b9522ca96b362f0f.jpg!q70.dpg.webp" loaded="42" style="max-width: 390px;"><a target="_self" href="//coupon.m.jd.com/coupons/show.action?key=dd22d062aa994bdfbe3191cde4fbb7cd&amp;roleId=108286927&amp;to=ecovacs.jd.com&amp;sceneval=2&amp;jxsid=16856232465417542442" style="background-color: rgb(255, 255, 255);"><img src="//img30.360buyimg.com/popWareDetail/jfs/t1/109553/3/39902/26714/6470a52dF50399555/7ca6736e7c6e8b71.jpg!q70.dpg.webp" class="" loaded="41" style="max-width: 390px;"></a><a target="_self" href="//pro.m.jd.com/mall/active/3CqyVj3kCMiBeWvW5QpzhTdmH3V3/index.html?sceneval=2&amp;jxsid=16856232465417542442"><img src="//img30.360buyimg.com/popWareDetail/jfs/t1/192943/28/37494/37588/647737c3F955ac0c2/cca3cbe71f09db2d.jpg!q70.dpg.webp" class="" loaded="40" style="max-width: 390px;"></a><img src="//img30.360buyimg.com/popWareDetail/jfs/t1/126966/40/37247/112288/6470a709F28bba716/0338159d8a459b60.jpg!q70.dpg.webp" loaded="39" style="max-width: 390px;"><a target="_self" href="//pro.m.jd.com/mall/active/DUXnUibJoCU5iCYXg8PHePSuFbA/index.html?sceneval=2&amp;jxsid=16856232465417542442" style="background-color: rgb(255, 255, 255);"><img src="//img30.360buyimg.com/popWareDetail/jfs/t1/222920/4/23068/39827/6461c03bFc87dd082/46bfb6596ec5ff88.jpg!q70.dpg.webp" class="" loaded="38" style="max-width: 390px;"></a></p>'

storeProductDefault = '{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","sliderImages":["http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg","http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg"],"videoLink":"","sliderImage":"[\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg\\",\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg\\"]","storeName":"123","storeInfo":"123","keyword":"123 123","cateIds":[246,248,258,488],"cateId":"246,248,258,488","unitName":"123","sort":0,"giveIntegral":0,"ficti":0,"isShow":false,"isBenefit":false,"isNew":true,"isGood":false,"isHot":true,"isBest":false,"tempId":1,"attrValue":[{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","price":0,"cost":0,"otPrice":0,"stock":0,"barCode":"","weight":0,"volume":0,"attrValue":"{\\"规格\\":\\"默认\\"}"}],"attr":[{"attrName":"规格","attrValues":"默认","id":0}],"selectRule":"","isSub":false,"content":"<p>12356</p>","specType":false,"id":0,"couponIds":[],"coupons":[],"activity":["默认","秒杀","砍价","拼团"]}'


# typeId, movie_count, rating, vote_count,start
def get_url_data_in_ranking_list2(typeR=1,page=1,limit=50,storeProductJ=None):

    try:
        context = _create_unverified_context()  # 屏蔽ssl证书
        headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.188 Safari/537.36 CrKey/1.54.250320',
            'Referer': 'https://www.jd.com/'
        }
        body='{"type":"'+str(typeR)+'","page":'+str(page)+',"limit":'+str(limit)+',"pin":"","uuid":"165547743008963864114","source":"pc-home"}'
        param='appid=www-jd-com&body='+body+'&clientVersion=1.0.0&client=pc&functionId=pc_home_feed_tab&t=1685702976431&uuid=76161171.165547743008963864114.1655477430.1685622023.1685702957.20&loginType=3&x-api-eid-token=jdd03WK4SUWB2OK3QS3BSWUE5EEEDUVX4SQC4LRSXFFYEH4NLVG77DPRXRZUFJ2Q6ZFYA2AAYLWAOHQQNVHIKJZU44U4MCQAAAAMIPO45ZGQAAAAAC3DJMYNVKFHYXQX&callback=jsonpMoreTabGoods&_=1685702976431'
        url = 'https://api.m.jd.com/?' + param
        # print(url)
        req = urllib.request.Request(url=url, headers=headers)
        f = urllib.request.urlopen(req, context=context)
        response = f.read().decode("utf-8")

        response = response[len("jsonpMoreTabGoods("):len(response) - 1]
        jsonData = loads(response)  # 将json转为python对象
        print(jsonData)

        storeProductL = list()



        for product in jsonData['data']:
            storeProduct = loads(storeProductJ)
            info = product['productBaseInfo']
            price = info['opPrice'] or info['jdPrice'] or info['pPrice'] or info['pcpPrice'] or info['mkPrice']
            image = 'https:'+info['image']
            name = info['name']

            storeProduct['image'] = image
            storeProduct['storeName'] = name
            storeProduct['unitName'] = '京东爬取商品数据'
            storeProduct["attrValue"][0]["image"] = image
            storeProduct["attrValue"][0]["price"] = price
            storeProduct["attrValue"][0]["otPrice"] = price

            # print("y")
            keywords = analyse.extract_tags(name, topK=3, withWeight=True, allowPOS=())
            storeProduct["sliderImages"] = [image]
            storeProduct["sliderImage"] = "[" + image + "," + image + "]"
            storeProduct["keyword"] = ""
            for tuple in keywords:
                storeProduct["keyword"] = storeProduct["keyword"] + " " + tuple[0]
            storeProduct["storeInfo"] = name
            storeProduct['content'] = storeContent

            # print(storeProduct)
            storeProductL.append(storeProduct)
        return storeProductL
    except Exception as ex:
        print(ex)
        err_str = "出现未知异常：{}".format(ex)
        return [err_str, -1]
#智能先锋
storeProduct1='{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","sliderImages":["http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg","http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg"],"videoLink":"","sliderImage":"[\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg\\",\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg\\"]","storeName":"123","storeInfo":"123","keyword":"123 123","cateIds":[246,248,258,488,297,298,273,275,276,277,279],"cateId":"246,248,258,488,297,298,273,275,276,277,279","unitName":"123","sort":0,"giveIntegral":0,"ficti":0,"isShow":false,"isBenefit":false,"isNew":true,"isGood":false,"isHot":true,"isBest":false,"tempId":1,"attrValue":[{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","price":0,"cost":0,"otPrice":0,"stock":0,"barCode":"","weight":0,"volume":0,"attrValue":"{\\"规格\\":\\"默认\\"}"}],"attr":[{"attrName":"规格","attrValues":"默认","id":0}],"selectRule":"","isSub":false,"content":"<p>12356</p>","specType":false,"id":0,"couponIds":[],"coupons":[],"activity":["默认","秒杀","砍价","拼团"]}'
#时尚达人
storeProduct2='{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","sliderImages":["http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg","http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg"],"videoLink":"","sliderImage":"[\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg\\",\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg\\"]","storeName":"123","storeInfo":"123","keyword":"123 123","cateIds":[246,248,258,488,288,289],"cateId":"246,248,258,488,288,289","unitName":"123","sort":0,"giveIntegral":0,"ficti":0,"isShow":false,"isBenefit":false,"isNew":true,"isGood":false,"isHot":true,"isBest":false,"tempId":1,"attrValue":[{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","price":0,"cost":0,"otPrice":0,"stock":0,"barCode":"","weight":0,"volume":0,"attrValue":"{\\"规格\\":\\"默认\\"}"}],"attr":[{"attrName":"规格","attrValues":"默认","id":0}],"selectRule":"","isSub":false,"content":"<p>12356</p>","specType":false,"id":0,"couponIds":[],"coupons":[],"activity":["默认","秒杀","砍价","拼团"]}'
#居家优品
storeProduct3='{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","sliderImages":["http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg","http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg"],"videoLink":"","sliderImage":"[\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg\\",\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg\\"]","storeName":"123","storeInfo":"123","keyword":"123 123","cateIds":[246,248,258,488,285,286,287],"cateId":"246,248,258,488,285,286,287","unitName":"123","sort":0,"giveIntegral":0,"ficti":0,"isShow":false,"isBenefit":false,"isNew":true,"isGood":false,"isHot":true,"isBest":false,"tempId":1,"attrValue":[{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","price":0,"cost":0,"otPrice":0,"stock":0,"barCode":"","weight":0,"volume":0,"attrValue":"{\\"规格\\":\\"默认\\"}"}],"attr":[{"attrName":"规格","attrValues":"默认","id":0}],"selectRule":"","isSub":false,"content":"<p>12356</p>","specType":false,"id":0,"couponIds":[],"coupons":[],"activity":["默认","秒杀","砍价","拼团"]}'
#超市百货
storeProduct4='{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","sliderImages":["http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg","http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg"],"videoLink":"","sliderImage":"[\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg\\",\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg\\"]","storeName":"123","storeInfo":"123","keyword":"123 123","cateIds":[246,248,258,488,302],"cateId":"246,248,258,488,302","unitName":"123","sort":0,"giveIntegral":0,"ficti":0,"isShow":false,"isBenefit":false,"isNew":true,"isGood":false,"isHot":true,"isBest":false,"tempId":1,"attrValue":[{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","price":0,"cost":0,"otPrice":0,"stock":0,"barCode":"","weight":0,"volume":0,"attrValue":"{\\"规格\\":\\"默认\\"}"}],"attr":[{"attrName":"规格","attrValues":"默认","id":0}],"selectRule":"","isSub":false,"content":"<p>12356</p>","specType":false,"id":0,"couponIds":[],"coupons":[],"activity":["默认","秒杀","砍价","拼团"]}'
#进口好物
storeProduct5 = '{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","sliderImages":["http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg","http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg"],"videoLink":"","sliderImage":"[\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/9e97ddf78f804d50b985bdcf666478ebj76hbf3gpp.jpg\\",\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b9f86161d09a44d29f7e5083d402245321764lzsus.jpg\\"]","storeName":"123","storeInfo":"123","keyword":"123 123","cateIds":[246,248,258,488],"cateId":"246,248,258,488","unitName":"123","sort":0,"giveIntegral":0,"ficti":0,"isShow":false,"isBenefit":false,"isNew":true,"isGood":false,"isHot":true,"isBest":false,"tempId":1,"attrValue":[{"image":"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png","price":0,"cost":0,"otPrice":0,"stock":0,"barCode":"","weight":0,"volume":0,"attrValue":"{\\"规格\\":\\"默认\\"}"}],"attr":[{"attrName":"规格","attrValues":"默认","id":0}],"selectRule":"","isSub":false,"content":"<p>12356</p>","specType":false,"id":0,"couponIds":[],"coupons":[],"activity":["默认","秒杀","砍价","拼团"]}'
storeProductD=[storeProduct1,storeProduct2,storeProduct3,storeProduct4,storeProduct5]

#[1,2,3,4,5] 类别 1智能先锋 2时尚达人 3居家优品 4超市百货 5进口好物
typeRT=['智能先锋', '时尚达人','居家优品' ,'超市百货' ,'进口好物']
# page=1
limit=25
# typeR=2
#后面三种类别 3，4，5一个爬取10*25个
# for typei in range(3,6):
#     for page in range(0, 10):
#         try:
#             storeProductL = get_url_data_in_ranking_list2(typeR=typei, page=page + 1, limit=limit,storeProductJ=storeProductD[typei - 1])
#             # 将美食信息写入JSON文件
#             with open(f'../storeJsonData/JD{typeRT[typei - 1]}{page + 1}.json', 'w', encoding='utf-8') as f:
#                 json.dump(storeProductL, f, ensure_ascii=False, indent=4)
#         except Exception as ex:
#             print("爬取完了......")
#             print(ex)
#     time.sleep(5)
#1类别爬取20*25个
# for page in range(0, 20):
#     try:
#         storeProductL = get_url_data_in_ranking_list2(typeR=1, page=page + 1, limit=limit,storeProductJ=storeProductD[1-1])
#         # 将美食信息写入JSON文件
#         with open(f'../storeJsonData/JD{typeRT[1 - 1]}{page + 1}.json', 'w', encoding='utf-8') as f:
#             json.dump(storeProductL, f, ensure_ascii=False, indent=4)
#     except Exception as ex:
#         print("爬取完了......")
#         print(ex)
# time.sleep(10)

#2类别爬取18*25个
for page in range(0, 18):
    try:
        storeProductL = get_url_data_in_ranking_list2(typeR=2, page=page + 1, limit=limit,storeProductJ=storeProductD[2-1])
        # 将美食信息写入JSON文件
        with open(f'../storeJsonData/JD{typeRT[2 - 1]}{page + 1}.json', 'w', encoding='utf-8') as f:
            json.dump(storeProductL, f, ensure_ascii=False, indent=4)
    except Exception as ex:
        print("爬取完了......")
        print(ex)

